#include "downloadlistwidgets.h"
#include "downloadunits.h"
#include "downloadlistitemwidget.h"
#include "downloadobject.h"
#include <QDebug>

DownloadListWidgets::DownloadListWidgets(QWidget *parent) :
    QListWidget(parent)
{
    setAttribute(Qt::WA_TranslucentBackground, true);

    setStyleSheet(DownloadObject::QScrollBarStyle
                  + "QListWidget{border:none;}");
    connect(this,SIGNAL(downloadStateChanged(bool&)),parent,
                 SLOT(downloadStateChanged(bool&)));
    connect(this,SIGNAL(itemClicked(QListWidgetItem*)),
                 SLOT(itemClickedPressed(QListWidgetItem*)));
    connect(this,SIGNAL(itemDoubleClicked(QListWidgetItem*)),
                 SLOT(itemDoubleClickedPressed(QListWidgetItem*)));

    setTransparent(100);
    m_maxDownloadCount = 0;
}

DownloadListWidgets::~DownloadListWidgets()
{
    clearItems();
}

void DownloadListWidgets::setTransparent(int angle)
{
    QPalette pal = palette();
    pal.setBrush(QPalette::Base,QBrush(QColor(255,255,255,angle)));
    setPalette(pal);
}

void DownloadListWidgets::clearItems()
{
    while(!m_itemList.isEmpty())
    {
        delete m_itemList.takeLast();
    }
    clear();
}

void DownloadListWidgets::addItemToList(const QStringList& path)
{
    foreach(QString pa, path)
    {
        pa = pa.trimmed();
        if( !pa.isEmpty() && !m_urls.contains(pa) )
        {
            addItemToList(pa);
            m_urls << pa;
        }
    }
}

void DownloadListWidgets::addItemToList(const QString& path)
{
    if(path.isEmpty()) return;

    DownloadUnits *unit = new DownloadUnits(path,this);
    m_itemList << unit;
    DownloadListItemWidget *widget = unit->getDownloadItemWidget();
    QListWidgetItem *item = new QListWidgetItem(this);
    setItemWidget(item, widget);
    item->setSizeHint(widget->size());
}

void DownloadListWidgets::pause()
{
    if(m_itemList.isEmpty() || currentRow() <= -1)
        return;
    m_itemList[currentRow()]->pause();
    stateChanged();
}

void DownloadListWidgets::start()
{
    if( m_itemList.isEmpty() || currentRow() <= -1)
        return;

    DownloadUnits* units = m_itemList[currentRow()];
    if( ++m_maxDownloadCount <= 3)
    {
        units->start();
        stateChanged();
    }
    else
    {
        units->setStateChanged(tr("D_Queue"));
        m_downloadQueue << units->getUrl();
    }
}

void DownloadListWidgets::deleteItemFromList()
{
    int index = currentRow();
    if(index <= -1) return;

    if( m_downloadQueue.contains(m_itemList[index]->getUrl()) )
    {
        getTopUrlToDownload();
    }
    m_urls.removeAt(index);
    delete m_itemList.takeAt(index);
    delete takeItem(index);
}

void DownloadListWidgets::downloadingFinished(const QString& url)
{
    int index = getIndexFromUrl(url);
    if(index == -1) return;

    m_urls.removeAt(index);
    delete m_itemList.takeAt(index);
    delete takeItem(index);
    qDebug()<<index<<" downloadingFinished";

    getTopUrlToDownload();
}

void DownloadListWidgets::itemClickedPressed(QListWidgetItem*)
{
    if(currentRow() <= -1) return;
    stateChanged();
}

void DownloadListWidgets::itemDoubleClickedPressed(QListWidgetItem*)
{
    start();
}

void DownloadListWidgets::stateChanged()
{
    int state = m_itemList[currentRow()]->getState();
    bool s;
    (state != 1 ) ? s = true : s = false;
    emit downloadStateChanged(s);
}

void DownloadListWidgets::getTopUrlToDownload()
{
    int index;
    --m_maxDownloadCount;
    if( !m_downloadQueue.isEmpty() && !m_itemList.isEmpty() &&
        ( (index = getIndexFromUrl(m_downloadQueue[0])) != -1 ))
    {
        m_itemList[index]->start();
    }
}

int DownloadListWidgets::getIndexFromUrl(const QString &url)
{
    int index = -1;
    foreach(QString s, m_urls)
    {
        ++index;
        if(s == url) break;
    }
    return index;
}
